﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using XYAuto.MonitorData.Chitunion.Bll.MonitorNotice;
using XYAuto.MonitorData.Chitunion.Bll.MonitorNotice.Interface;
using XYAuto.MonitorData.Chitunion.Infrastruct.Verification;

namespace XYAuto.MonitorData.Chitunion.Bll.Provider
{
    public class MonitorProvider
    {
        public ReturnValue DoMonitor()
        {
            var retValue = new ReturnValue();
            var list = Dal.MonitorTaskInfo.Instance.GeTaskInfos();

            list.ForEach(t =>
            {
                //todo:后期需要优化存储线程
                //Task.Run(() => GetExcuteSql(t));
                GetExcuteSql(t);
            });

            return retValue;
        }

        private void GetExcuteSql(Entities.MonitorTaskInfo item)
        {
            var count = new Dal.DoMonitor(item.DataConnect, item.MonitorSql).ExcuteSql();
            if (count > item.AchieveCount)
            {
                //todo:发送预警通知
                new SendProvider(new SendDto
                {
                    MonitorId = item.MonitorId,
                    ToUser = item.NoticeToValue,
                    NoticeKey = item.NoticeKey,
                    Body = item.NoticeDesc,
                    Subject = item.MonitorName
                }).ToSend();
            }

        }
    }
}
